package org.bulldog.playlist.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by zhanghongbin on 2017/9/6.
 *
 * @author zhanghongbin
 * @version 1.0
 */
public final class DateUtil {

    //节目最短播放时间 30s
    public static final int MIN_PLAY_DURATION = 30;

    private DateUtil() {

    }

    /**
     * time 是否在startTime 和endTime 之间 (结束时间如果和time相等返回false)
     *
     * @param time
     * @param startTime
     * @param endTime
     * @return
     */
    public static final boolean isBetween(String time, String startTime, String endTime) {
        //不能把SimpleDateFormat 声明成静态的，多线程会有问题
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar date = Calendar.getInstance();
        try {
            date.setTime(simpleDateFormat.parse(time));
            Calendar after = Calendar.getInstance();
            after.setTime(simpleDateFormat.parse(startTime));
            Calendar before = Calendar.getInstance();
            before.setTime(simpleDateFormat.parse(endTime));
            if (date.compareTo(after) != -1 && date.before(before)) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 结束时间如果和time相等返回true)
     *
     * @param time
     * @param startTime
     * @param endTime
     * @return
     */
    public static final boolean isBetween2(String time, String startTime, String endTime) {
        //不能把SimpleDateFormat 声明成静态的，多线程会有问题
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar date = Calendar.getInstance();
        try {
            date.setTime(simpleDateFormat.parse(time));
            Calendar after = Calendar.getInstance();
            after.setTime(simpleDateFormat.parse(startTime));
            Calendar before = Calendar.getInstance();
            before.setTime(simpleDateFormat.parse(endTime));
            if (date.compareTo(after) != -1 && date.compareTo(before) <= 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 日期比较
     *
     * @param date1
     * @param date2
     * @return data1 早于 data2 返回 -1 ，晚于 返回 1 相当返回0
     */
    public static final int compare(String date1, String date2) {
        //不能把SimpleDateFormat 声明成静态的，多线程会有问题
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return simpleDateFormat.parse(date1).compareTo(simpleDateFormat.parse(date2));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static boolean dataCompare(String begin, String end) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        Calendar c3 = Calendar.getInstance();
        try {
            String now = df.format(new Date());
            c1.setTime(df.parse(now));
            c2.setTime(df.parse(begin));
            c3.setTime(df.parse(end));
            if (c1.compareTo(c2) >= 0 && c1.compareTo(c3) <= 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("格式不正确");
        }
        return false;
    }

    /**
     * 获取当前时间
     *
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static final String now() {
        //不能把SimpleDateFormat 声明成静态的，多线程会有问题
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(new Date());
    }

    public static final String now(String pattern){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(new Date());
    }

    /**
     * 获取当前周几
     *
     * @return
     */
    public static final int week() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        return cal.get(Calendar.DAY_OF_WEEK) - 1;
    }


    public static final String add(int second) {
        //不能把SimpleDateFormat 声明成静态的，多线程会有问题
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, second);
        Date date = cal.getTime();
        return simpleDateFormat.format(date);
    }

    /**
     * @param time   yyyy-MM-dd HH:mm:ss
     * @param second 秒
     * @return
     */
    public static final String add(String time, int second) {
        //不能把SimpleDateFormat 声明成静态的，多线程会有问题
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return simpleDateFormat.format(simpleDateFormat.parse(time).getTime() + second * 1000);
        } catch (Exception e) {
        }
        return time;
    }


    /**
     * 两个时间相减
     *
     * @param startTime yyyy-MM-dd HH:mm:ss
     * @param endTime   yyyy-MM-dd HH:mm:ss
     * @return 返回值为秒
     */
    public static final int minus(String startTime, String endTime) {
        //不能把SimpleDateFormat 声明成静态的，多线程会有问题
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date start = simpleDateFormat.parse(startTime);
            Date end = simpleDateFormat.parse(endTime);
            return (int) (end.getTime() - start.getTime()) / 1000;
        } catch (Exception e) {
        }
        //如果出问题默认返回30s
        return MIN_PLAY_DURATION;
    }

    public static Date add(Date date, int num) {
        Calendar startDT = Calendar.getInstance();
        startDT.setTime(date);
        startDT.add(Calendar.DAY_OF_MONTH, num);
        return startDT.getTime();
    }
}
